#------------------------------------------------------------------------------#
################ Cronbach's Alpha: Psychological Variables #####################
#------------------------------------------------------------------------------#

rm(list = ls())

# Setting directory: adjust accordingly to 

setwd()

#------------------------------------------------------------------------------#
### Loading and installing necessary packages ##################################
#------------------------------------------------------------------------------#

# Installing required packages 

packages <- c('tidyverse', 'readxl', 'stringr', 
              'lmtest', 'sandwich', 'stargazer',
              'haven', 'survey', 'GDAtools',
              'weights','knitr','broom','gtools',
              'huxtable', 'compareGroups', 'lfe',
              'kableExtra', 'interplot', 'gridExtra',
              'miceadds', 'ggforce', 'httr', 'gdata',
              'rmarkdown', 'fixest', 'naniar', 'psych',
              'GPArotation', 'jtools', 'formattable',
              'openxlsx', 'fastDummies', 'mfx', 'margins',
              'corrplot', 'grid', 'gridExtra', 'broom.mixed',
              'erer', 'fixest', 'ggstance', 'lme4', 'lmerTest',
              'nlme', 'psych', 'spatstat', 'marginaleffects', 'texreg',
              'ggstance')

# Checking if is installed (and install if not)

packages.check <- lapply(
  packages,
  FUN = function(x) {
    if (!require(x, character.only = TRUE)) {
      install.packages(x, dependencies = TRUE)
      library(x, character.only = TRUE)
    }
  }
)

rm(packages, packages.check)

#------------------------------------------------------------------------------#
### Loading data base  #########################################################
#------------------------------------------------------------------------------#

Base <- readRDS(file = "data/Base.rds")

#------------------------------------------------------------------------------#
### Calculating Alpha  #########################################################
#------------------------------------------------------------------------------#

# Individualism Worldview
Base %>% 
  dplyr::select(P23_1, P23_2, P23_3, P23_4, P23_5) %>%
  alpha()

# Egalitarian Worldview
Base %>% 
  dplyr::select(P23_6, P23_7, P23_8, P23_10) %>%
  alpha()

# New Ecological Paradigm (NEP)
Base %>% 
  dplyr::select(P12_1, P12_2, P12_3, P12_4) %>%       
  alpha(keys = c(1, -1, 1, 1))

Base %>% 
  dplyr::select(P12_1, P12_3, P12_4) %>%       
  alpha()

Base %>% 
  dplyr::select(P12_1, P12_2, P12_4) %>%       
  alpha(keys = c(1, -1, 1))

Base %>% 
  dplyr::select(P12_1, P12_4) %>%      
  alpha()

factor_NEP <- Base %>% 
  mutate(P12_2 = 5 - P12_2) %>%
  dplyr::select(P12_1, P12_2, P12_3, P12_4)

fa(factor_NEP, nfactors = 4, rotate = "oblimin") 
fa(factor_NEP, nfactors = 1, rotate = "oblimin") 

Base %>%
  dplyr::select(`Belief climate change (binary)`, cau, imp) %>%
  alpha()
